package com.qf.bbapi.service.impl;

import com.qf.bbapi.config.RedisKeyConfig;
import com.qf.bbapi.dao.SmsLogDao;
import com.qf.bbapi.dto.UserAddDto;
import com.qf.bbapi.entity.SmsLog;
import com.qf.bbapi.service.intf.SmsLogService;
import com.qf.bbapi.type.SmsLogType;
import com.qf.bbapi.util.AliSmsUtil;
import com.qf.bbapi.util.JedisUtil;
import com.qf.bbapi.util.NumUtil;
import com.qf.bbapi.util.StringUtil;
import com.qf.bbapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SmsLogServiceImpl implements SmsLogService {
    @Resource
    private SmsLogDao smsLogDao;

    @Override
    public R sendByPhone(String phone) {
        if (StringUtil.isPhone(phone)) {
            //生成验证码
            String s = NumUtil.createCode(6) + "";
            System.out.println(s);
            //发送验证码
            //if(AliSmsUtil.sendSmsCode(phone, s, AliSmsUtil.SMS_TEM_LOGIN)){
            if(StringUtil.isNotEmpty(s)){
                //记录验证码，有效期为1分钟
                JedisUtil.addString(RedisKeyConfig.SMS_CODE_LOGIN + phone, s, 60);
                //添加短信服务日志
                smsLogDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(),"成功发送用手机号登录的验证码",1));
                return R.ok();
            }else{
                smsLogDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(),"失败",2));
                return R.fail();
            }
            //}
        }

        return R.fail("手机号格式错误");

    }

    @Override
    public R sendByFind(String phone) {
        if (StringUtil.isPhone(phone)) {
            //生成验证码
            String s = NumUtil.createCode(6) + "";
            System.out.println(s);
            //发送验证码
            //if(AliSmsUtil.sendSmsCode(phone, s, AliSmsUtil.SMS_TEM_LOGIN)){
            if(StringUtil.isNotEmpty(s)){
                //记录验证码，有效期为1分钟
                JedisUtil.addString(RedisKeyConfig.SMS_CODE_FIND + phone, s, 60);
                //添加短信服务日志
                smsLogDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(),"成功发送用密码找回的验证码",1));
                return R.ok();
            }else{
                smsLogDao.insert(new SmsLog(phone, SmsLogType.密码找回.getValue(),"失败",2));
                return R.fail();
            }
            //}
        }
        return R.fail("手机号格式错误");
    }

    @Override
    public R sendByRegist(String phone) {
        if (StringUtil.isPhone(phone)) {
            //生成验证码
            String s = NumUtil.createCode(6) + "";
            System.out.println(s);
            //发送验证码
            //if(AliSmsUtil.sendSmsCode(phone, s, AliSmsUtil.SMS_TEM_LOGIN)){
            if(StringUtil.isNotEmpty(s)){
                //记录验证码，有效期为1分钟
                JedisUtil.addString(RedisKeyConfig.SMS_CODE_ADD + phone, s, 60);
                //添加短信服务日志
                smsLogDao.insert(new SmsLog(phone, SmsLogType.注册验证码.getValue(),"成功发送用户注册的验证码",1));
                return R.ok();
            }else{
                smsLogDao.insert(new SmsLog(phone, SmsLogType.注册验证码.getValue(),"失败发送用户注册的验证码",2));
                return R.fail();
            }
            //}
        }
        return R.fail("手机号格式错误");
    }
}
